// 版权归陈超所有


#include "UI/WidgetController/CC_OverlayWidgetController.h"

#include "AbilitySystem/CC_AbilitySystemComponent.h"
#include "AbilitySystem/CC_AttributeSet.h"

void UCC_OverlayWidgetController::BroadcastInitialValues()
{
	if (CC_AttributeSet == nullptr)
	{
		CC_AttributeSet = Cast<UCC_AttributeSet>(AttributeSet);
	}
	//广播属性变化
	OnHealthChanged.Broadcast(CC_AttributeSet->GetHealth());
	OnMaxHealthChanged.Broadcast(CC_AttributeSet->GetMaxHealth());
	OnManaChanged.Broadcast(CC_AttributeSet->GetMana());
	OnMaxManaChanged.Broadcast(CC_AttributeSet->GetMaxMana());
}

void UCC_OverlayWidgetController::BindCallbacksToDependencies()
{
	if (CC_AttributeSet == nullptr)
	{
		CC_AttributeSet = Cast<UCC_AttributeSet>(AttributeSet);
	}
	/*监听属性变化*/
	AbilitySystemComponent->GetGameplayAttributeValueChangeDelegate(CC_AttributeSet->GetHealthAttribute()) 
		.AddUObject(this, &UCC_OverlayWidgetController::HealthChanged);
	
	AbilitySystemComponent->GetGameplayAttributeValueChangeDelegate(CC_AttributeSet->GetMaxHealthAttribute()) 
		.AddUObject(this, &UCC_OverlayWidgetController::MaxHealthChanged);
	
	AbilitySystemComponent->GetGameplayAttributeValueChangeDelegate(CC_AttributeSet->GetManaAttribute()) 
		.AddUObject(this, &UCC_OverlayWidgetController::ManaChanged);
	
	AbilitySystemComponent->GetGameplayAttributeValueChangeDelegate(CC_AttributeSet->GetMaxManaAttribute()) 
		.AddUObject(this, &UCC_OverlayWidgetController::MaxManaChanged);

	/*EffectActorTag信息*/
	 if (CC_AbilitySystemComponent == nullptr)
	{
		CC_AbilitySystemComponent = Cast<UCC_AbilitySystemComponent>(AbilitySystemComponent);
	}
	
	CC_AbilitySystemComponent->OnEffectAssetTags.AddLambda(
		[](const FGameplayTagContainer& AssetTags)
		{
			for(const FGameplayTag& Tag : AssetTags)
			{
				//将tag广播给Widget Controller
				const FString Msg = FString::Printf(TEXT("GE Tag in Widget Controller: %s"), *Tag.ToString()); //获取Asset Tag
				GEngine->AddOnScreenDebugMessage(-1, 8.f, FColor::Cyan, Msg); //打印到屏幕上 -1 不会被覆盖
			}
		}
	);
}

void UCC_OverlayWidgetController::HealthChanged(const FOnAttributeChangeData& Data) const
{
	OnHealthChanged.Broadcast(Data.NewValue);
}

void UCC_OverlayWidgetController::MaxHealthChanged(const FOnAttributeChangeData& Data) const
{
	OnMaxHealthChanged.Broadcast(Data.NewValue);
}

void UCC_OverlayWidgetController::ManaChanged(const FOnAttributeChangeData& Data) const
{
	OnManaChanged.Broadcast(Data.NewValue);
}

void UCC_OverlayWidgetController::MaxManaChanged(const FOnAttributeChangeData& Data) const
{
	OnMaxManaChanged.Broadcast(Data.NewValue);
}

